﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using GsifWeb4.Models;
using GsifWeb4;
using System.Web.Mvc;

namespace GsifWeb4.ViewModels
{
    public class PhoneListViewModel
    {
        public List<laget_gsif> Players { get; set; }
        public List<CheckBoxItem> Teams { get; set; }

        public PhoneListViewModel(List<StatusValue> selectedTeams, gsifdbEntities gsifDB)
        {
            Teams = new List<CheckBoxItem>();

            foreach (var team in Team.AllTeams)
            {
                bool isPlayersTeam = selectedTeams.Contains(team);
                Teams.Add(new CheckBoxItem() { Text = team.Text, Checked = isPlayersTeam });
            }
            string[] dbValueTeams = selectedTeams.Select(s => s.DbValue).ToArray();
            Players = gsifDB.laget_gsif.Where(p => dbValueTeams.Contains(p.Lag)).ToList();
            
            //// Använd predicateBuilder för att bygga upp en lista med OR 
            //var searchPredicate = PredicateBuilder.False<laget_gsif>();
            //foreach (var team in selectedTeams)
            //{
            //    string tmpValue = team.DbValue;
            //    searchPredicate = searchPredicate.Or(p => p.Lag == tmpValue);
            //}

            //// Applicera alla Where satser
            //Players = gsifDB.laget_gsif.AsExpandable()
            //    .Where(searchPredicate)
            //    .Where(p => p.Inaktiv != InActivePlayer.InActive.DbValue)
            //    .OrderBy(p => p.FNamn)
            //    .ToList();
        }
    }
}